package com.ibm.spatiotemp.utils;

import org.apache.log4j.Logger;

/*
 * Thread to check server status in database periodically and stop server when getting turn-off command (RODBStatus = 1)
 */
public class FlushController extends Thread {		
	/**
	 * Reference for log4j
	 */
	private final static Logger logger = Logger.getLogger(FlushController.class);
	/**
	 * The tag to indicate if the thread should be stopped.
	 */
	private boolean stopFlag = false;
	/**
	 * The sleep time for next check.
	 */
	private long sleepTime;
	/**
	 * DBThreadPool
	 */
	private DBThreadPool dbPool;
	
	public FlushController(int sleepTime, DBThreadPool dbPool) {
		super();	
		this.sleepTime = sleepTime;
		this.dbPool = dbPool;
	}	
		
	
	@Override
	public void run() {
		logger.info("FLushController is running......");
		while (!stopFlag) {
			try {
				sleep(sleepTime);
			} catch (InterruptedException e) {
				if (stopFlag) {
					logger.info("FlushController Thread is stopped.");							
					break;
				}
			}
			dbPool.flush();
		};
		logger.info("FLushController is finished.");
	}
		
	public boolean isStopFlag() {
		return stopFlag;
	}

	public synchronized void setStopFlag(boolean stopFlag) {
		this.stopFlag = stopFlag;
	}	
	
} 
